﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System.Collections;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;

namespace QuanLyThuNgan
{
    public partial class frm_DangKyCa : DevExpress.XtraEditors.XtraForm
    {
        public frm_DangKyCa()
        {
            InitializeComponent();
        }
        CaThuNganCTL catnctl = new CaThuNganCTL();
        CaThuNganNhapCTL catnNhap = new CaThuNganNhapCTL();
        DataTable _dtndung = new DataTable(); 
        string _tenseach, _columnseach;
        int idcalamviec=-1;
        public static string madonvi = "";
        public static int idcathungan = -1;
        string sql_update = ""; string sql_insert = "", sql_delete="";
        private void frm_DangKyCa_Load(object sender, EventArgs e)
        {  
            NguoiDungCTL ctlND = new NguoiDungCTL();
            _dtndung = ctlND.SelectALL_NoActive();

            dtpNgay.DateTime = Uit.it_Date.CurrentDate2();
            DMCaLamViecCTL clvctl = new DMCaLamViecCTL();
            NguoiDungCTL ndctl = new NguoiDungCTL();
            DonVi_Khac dvi= new DonVi_Khac();
            DataTable dtclv = clvctl.m_SelectAllDMCaLamViec();
            ClassHelp.m_LookUpEdit(dtclv, cboCa, "TenCaLamViec", "IDCaLamViec");
            gridControlNguoiDung.DataSource = _dtndung;
            DataTable dtDonVi = dvi.SelectDonVi_TheoCa();
            ClassHelp.m_LookUpEdit(dtDonVi, cboDonVi, "TenDonVi", "MaDonVi");
            DataTable dtload = catnctl.m_SelectAllCaThuNgan_all_moi(frmLogin.nd.IDNguoiDung);
            gridControlCaThuNgan.DataSource = dtload;
            if (dtload.Rows.Count == 0) return;
            cboCa.EditValue = dtload.Rows[0]["IDCaLAmViec"];
            cboDonVi.EditValue = dtload.Rows[0]["MaDonVi"];
            txtGhiChu.Text = dtload.Rows[0]["GhiChu"].ToString();
            dtpNgay.EditValue = dtload.Rows[0]["NgayDangKy"];

        }
    
        private void cboCa_EditValueChanged(object sender, EventArgs e)
        {
            idcalamviec = Uit.it_Parse.ToInteger(cboCa.EditValue);
            DataRowView dr = (DataRowView)cboCa.GetSelectedDataRow();
            if(dr!=null)
            txtGhiChu.Text = dr["GhiChu"].ToString();
        }
        private void txtGhiChu_EditValueChanged(object sender, EventArgs e)
        {

        }

        //private void gridViewNguoiDung_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
       // {
            //if (e.Info.IsRowIndicator)
            //{
            //    if (e.RowHandle < 0)
            //        e.Info.DisplayText = "";
            //    else
            //        e.Info.DisplayText = (e.RowHandle + 1).ToString();
            //}
            //else
            //    e.Info.DisplayText = "STT";
            //e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
       // }

        private void gridViewNguoiDung_ColumnFilterChanged(object sender, EventArgs e)
        {
            if (_tenseach != "")
                ClassHelp.m_gridView_seach(gridViewNguoiDung, _columnseach, _tenseach);
        }

        private void gridViewNguoiDung_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Value != null)
            {
                _tenseach = e.Value.ToString();
                _columnseach = e.Column.FieldName;
            }
        }
        private void btnDangKy_Click(object sender, EventArgs e)
        {
            sql_insert = "";
            if (kiemtradudkinsert() == true)
            {
                if (gridViewNguoiDung.GetRowCellValue(0, "IDHoTro").ToString().Trim() == "-1")
                {
                 //   gridViewNguoiDung.ActiveFilterEnabled = false;
                   // gridViewNguoiDung.ClearColumnsFilter();
                    //gridViewNguoiDung.OptionsFilter.Reset();
                    //gridViewNguoiDung.Columns["Chon"].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
                    CaThuNganDTO cathungInfo = new CaThuNganDTO();
                    CaThuNganNhapDTO cathunganNhapInfo = new CaThuNganNhapDTO();
                    cathungInfo.IIDCaLamViec = idcalamviec;
                    cathungInfo.IIDThuNgan = frmLogin.nd.IDNguoiDung;
                    cathungInfo.DaNgayDangKy = Uit.it_Parse.ToDateTime(Uit.it_Date.CurrentDate()).ToString("yyyy-MM-dd");
                    cathungInfo.SMaDonVi = madonvi;
                    sql_insert = sql_insert + " " + catnctl.m_InsertCaThuNgan_refe(cathungInfo);
                    sql_insert = sql_insert + " " + "declare @idd int  set @idd = (select max(IDCaThuNgan) from dbo.Utb_CaThuNgan)";
                    for (int i = 0; i < gridViewNguoiDung.RowCount; i++)
                    {
                        if (gridViewNguoiDung.GetRowCellValue(i, "Chon") != null)
                        {
                            if (gridViewNguoiDung.GetRowCellValue(i, "Chon").ToString() == "True")
                            {
                                int idngdung = Uit.it_Parse.ToInteger(gridViewNguoiDung.GetRowCellValue(i, "IDNguoiDung"));
                                sql_insert = sql_insert + " " + catnNhap.m_InsertCaThuNganNhap_("@idd", idngdung, true);
                            }
                        }
                    }
                }
            }
            else
                XtraMessageBox.Show("Bạn Chưa Điền Đủ Thông Tin","Thông Báo");
        }
        bool kiemtradudkinsert()
        {
            bool kt = true;
            if (cboCa.EditValue == null)
                kt = false;
            if (cboDonVi.EditValue == null)
                kt = false;
            bool ktra = false;
            for (int i = 0; i < gridViewNguoiDung.RowCount; i++)
            {
                if (gridViewNguoiDung.GetRowCellValue(i, "Chon") != null)
                {
                    if (gridViewNguoiDung.GetRowCellValue(i, "Chon").ToString() == "True")
                    {
                        ktra = true;
                    }
                }
            }
            if (!ktra)
                kt = false;
            return kt;
        }
        private void cboDonVi_EditValueChanged(object sender, EventArgs e)
        {
           
            madonvi = cboDonVi.EditValue.ToString();
             
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            NguoiDungCTL ctlND = new NguoiDungCTL();
            _dtndung = ctlND.SelectALL_NoActive();
            gridControlNguoiDung.DataSource = _dtndung;
            gridViewNguoiDung.ActiveFilterEnabled = false;
            gridViewNguoiDung.ClearColumnsFilter();
            gridViewNguoiDung.OptionsFilter.Reset();
            gridViewNguoiDung.Columns["Chon"].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
            gridControlCaThuNgan.DataSource = catnctl.m_SelectAllCaTN_All(frmLogin.nd.IDNguoiDung, Uit.it_Parse.ToDateTime(Uit.it_Date.CurrentDate2()).ToString("yyyy-MM-dd"));
            sql_update = "";
            sql_insert="";
            sql_delete = "";
            cboCa.EditValue = -1;
            //cboDonVi.Text= ;
            txtGhiChu.Text = "";
        }
        private void dtpNgay_EditValueChanged(object sender, EventArgs e)
        {
            DataTable dtload= catnctl.m_SelectAllCaTN_All(frmLogin.nd.IDNguoiDung, Uit.it_Parse.ToDateTime(dtpNgay.EditValue).ToString("yyyy-MM-dd"));
            gridControlCaThuNgan.DataSource = dtload;
            if (dtload.Rows.Count == 0) return;
            cboCa.EditValue= dtload.Rows[0]["IDCaLAmViec"];
            cboDonVi.EditValue = dtload.Rows[0]["MaDonVi"];
            txtGhiChu.Text = dtload.Rows[0]["GhiChu"].ToString();
        }
        private void btnTim_Click(object sender, EventArgs e)
        {

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (gridViewCaThuNgan.GetFocusedRowCellValue("IDThuNgan").ToString().Trim() == frmLogin.nd.IDNguoiDung.ToString().Trim())
          //  {
                //gridControlCaThuNgan.DataSource = catnctl.m_SelectAllCaTN();
                if (sql_insert != "")
                    if (Uit.it_SQL.ExecuteNonQuery(sql_insert, true))
                    {
                        XtraMessageBox.Show("Tạo Ca Thành Công");
                        sql_insert = "";
                    }
                if (sql_update != "")
                    if (Uit.it_SQL.ExecuteNonQuery(sql_update, false))
                    {
                        XtraMessageBox.Show("Update Thành Công");
                        sql_update = "";
                    }
                if (sql_delete != "")
                    if (Uit.it_SQL.ExecuteNonQuery(sql_delete, false))
                    {
                        XtraMessageBox.Show("Delete Thành Công");
                        sql_delete = "";
                    }
                gridControlCaThuNgan.DataSource = catnctl.m_SelectAllCaTN_All(frmLogin.nd.IDNguoiDung, Uit.it_Parse.ToDateTime(dtpNgay.EditValue).ToString("yyyy-MM-dd"));
            //}
            //else
            //    XtraMessageBox.Show("Bạn Không Có Quyền");

        }
        private void gridViewCaThuNgan_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                idcathungan = Uit.it_Parse.ToInteger(gridViewCaThuNgan.GetFocusedRowCellValue("IDCaThuNgan"));
                DataTable dtca = catnctl.m_SelectAll_NguoiDung_CaThuNgan(idcathungan,frmLogin.nd.IDNguoiDung);
                gridControlNguoiDung.DataSource = dtca;
                cboCa.EditValue = dtca.Rows[0]["IDCaLAmViec"];
                cboDonVi.EditValue = dtca.Rows[0]["MaDonVi"];

                if (gridViewCaThuNgan.GetFocusedRowCellValue("NgayHetCa").ToString().Trim() == "Đang Hoạt Động" && gridViewCaThuNgan.GetFocusedRowCellValue("IDThuNgan").ToString().Trim()==frmLogin.nd.IDNguoiDung.ToString().Trim())
                     gridViewNguoiDung.OptionsBehavior.ReadOnly = false;
                 else
                     gridViewNguoiDung.OptionsBehavior.ReadOnly = true;
            }
            catch{ return;
            } 
       }
      
        private void LoadLaiNguoiDungTheoCa(int IDCaThuNgan)
        {

        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            sql_update = "";
            gridViewNguoiDung.MoveNext();
            gridViewNguoiDung.MovePrev();
            for (int i = 0; i < gridViewNguoiDung.RowCount;i++)
            {
                if (gridViewNguoiDung.GetRowCellValue(i, "IDHoTro").ToString().Trim() == "1")
                {
                    if (gridViewNguoiDung.GetRowCellValue(i, "Chon").ToString().Trim() == "False")
                    {
                        CaThuNganNhapDTO cathungannhap = new CaThuNganNhapDTO();
                        cathungannhap.IIDCaThuNgan = idcathungan;
                        cathungannhap.IIDNguoiNhap = Uit.it_Parse.ToInteger(gridViewNguoiDung.GetRowCellValue(i, "IDNguoiDung"));
                        cathungannhap.BbNgungSuDung = false;// khi update  ve btinh trang =0 ma user doa khong co trong bang bafeemst thi xoa ra khoi bang cathungannhap
                        sql_update = sql_update + " " + catnNhap.m_DeleteCaThuNganNhap(cathungannhap);
                    }
                }
                if (gridViewNguoiDung.GetRowCellValue(i, "IDHoTro").ToString().Trim() == "0")
                {
                    if (gridViewNguoiDung.GetRowCellValue(i, "Chon").ToString().Trim() == "True")
                    {
                        CaThuNganNhapDTO cathungannhap = new CaThuNganNhapDTO();
                        cathungannhap.IIDCaThuNgan = idcathungan;
                        cathungannhap.IIDNguoiNhap = Uit.it_Parse.ToInteger(gridViewNguoiDung.GetRowCellValue(i, "IDNguoiDung"));
                        cathungannhap.BbNgungSuDung = true;
                        sql_update = sql_update + " " + catnNhap.m_InsertCaThuNganNhap_(cathungannhap.IIDCaThuNgan, cathungannhap.IIDNguoiNhap, cathungannhap.BbNgungSuDung);
                    }
                }
            }

        }

        private void gridViewNguoiDung_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            sql_delete = "";
            GridView view = gridViewCaThuNgan;
            int rowHandle = view.FocusedRowHandle;
            //if (gridViewCaThuNgan.GetFocusedRowCellValue("IDThuNgan").ToString().Trim() == frmLogin.nd.IDNguoiDung.ToString().Trim())
            //{
            if (rowHandle == -1)  return;
                if (view.IsGroupRow(rowHandle))
                {
                    CaThuNganDTO cainfo = new CaThuNganDTO();
                    DataRow drow = view.GetDataRow(rowHandle);
                    cainfo.IIDCaThuNgan = Uit.it_Parse.ToInteger(drow["IDCaThuNgan"]);
                    cainfo.IIDThuNgan = Uit.it_Parse.ToInteger(drow["IDThuNgan"]);
                    sql_delete = sql_delete + " " + catnctl.m_DeleteCaThuNgan(cainfo);
                    int childCount = view.GetChildRowCount(rowHandle);
                    for(int i=0; i<childCount; i++)
                    {
                        CaThuNganNhapDTO canhapinfo = new CaThuNganNhapDTO();
                        DataRowView dro = view.GetRow(i) as DataRowView;
                        canhapinfo.IIDNguoiNhap = Uit.it_Parse.ToInteger(dro["IDNguoiNhap"]);
                        canhapinfo.IIDCaThuNgan = Uit.it_Parse.ToInteger(dro["IDCaThuNgan"]);
                        sql_delete = sql_delete + " " + catnNhap.m_DeleteCaThuNganNhap(canhapinfo);

                     
                    }       
                  }
                else
                {
                    CaThuNganNhapDTO canhapinfo = new CaThuNganNhapDTO();
                    DataRow dro = view.GetDataRow(rowHandle);
                    canhapinfo.IIDNguoiNhap = Uit.it_Parse.ToInteger(dro["IDNguoiNhap"]);
                    canhapinfo.IIDCaThuNgan = Uit.it_Parse.ToInteger(dro["IDCaThuNgan"]);
                    sql_delete = sql_delete + " " + catnNhap.m_DeleteCaThuNganNhap(canhapinfo);

                }
            //}
            //else
            //    XtraMessageBox.Show("Bạn Không Có Quyền");

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThuTien_Click(object sender, EventArgs e)
        { 
            Form f = FormHelpers.CheckChildFormShow("frm_NhapChiPhiDichVuNgoaiTru", this);
            if (f == null)
            {
                frm_NhapChiPhiDichVuNgoaiTru frm = new frm_NhapChiPhiDichVuNgoaiTru();
                frm.MdiParent = this.ParentForm;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
            else
            {
                f.Activate();
                f.Focus();
            }
             this.Close();
        }

       
       
    }
}